package cn.chinasyq.photoquan.photo.adapter;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.photo.bean.PhotoEntity;
import cn.master.util.utils.ScreenUtil;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/11/24.
 */
public class PhotoListAdapter extends BaseAdapter {

    public List<PhotoEntity> getPhotoEntities() {
        return photoEntities;
    }

    public void setPhotoEntities(List<PhotoEntity> photoEntities) {
        if (photoEntities==null){
            photoEntities = new ArrayList<>();
        }
        this.photoEntities = photoEntities;
        notifyDataSetChanged();
    }

    public void addPhotoEntities(List<PhotoEntity> photoEntities) {
        this.photoEntities.addAll(photoEntities);
        notifyDataSetChanged();
    }

    private List<PhotoEntity> photoEntities;
    private int itemWidth, itemHeight;

    public PhotoListAdapter(Activity activity) {
        photoEntities = new ArrayList<>();
        DisplayMetrics m = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(m);
        itemWidth = (int) ((ScreenUtil.getScreenWidth(activity) - 3 * TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, m)) / 2);
        itemHeight = (int) (itemWidth * (9f / 16f));
    }

    @Override
    public int getCount() {
        return photoEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return photoEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotoEntity entity = (PhotoEntity) getItem(position);
        return getOneImageView(entity, convertView, parent, position);
    }


    private View getOneImageView(PhotoEntity entity, View convertView, ViewGroup parent, int position) {
        ViewHolderOneImage viewHolder = new ViewHolderOneImage();
        if (position % 2 == 0) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photography_exhibition, null);
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photography_exhibition2, null);
        }

        viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
        viewHolder.iv_image = (NetworkImageView) convertView.findViewById(R.id.iv_image);
        viewHolder.iv_image.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.iv_image.setLayoutParams(new LinearLayout.LayoutParams(itemWidth, itemHeight));
        convertView.setTag(viewHolder);
        viewHolder.tv_title.setText(entity.getTitle());
        viewHolder.iv_image.setImageUrl(entity.getImage(), VolleyHelper.getImageLoader());
        return convertView;
    }


    class ViewHolderOneImage {
        TextView tv_title;
        NetworkImageView iv_image;
    }

}
